package engine.loader;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import engine.Engine;
import engine.Logger;

public class ConfigFileLoader extends FileLoader {
	private HashMap<String, String> parameters;

	public ConfigFileLoader(Engine engine, String fileName) {
		super(engine, fileName);
		parameters = new HashMap<String, String>();
	}

	@Override
	protected void parseRule() throws IOException {
		BufferedReader reader = getFileReader();
		String line;
		String[] tokens;
		
		while ((line = reader.readLine()) != null) {
			if (line.isEmpty() || line.startsWith("//")) {
				continue;
			}
			
			tokens = line.split("=");
			
			if (tokens == null || tokens.length != 2) {
				log(Logger.WARNING, "Error loading setting: " + line);
				continue;
			}
			
			parameters.put(tokens[0].trim(), tokens[1].trim());
		}
		
		processParameters();
	}

	private void processParameters() {
		Iterator<String> keys = parameters.keySet().iterator();
		String key;
		
		while (keys.hasNext()) {
			key = keys.next();
			getEngine().setConfig(key, parameters.get(key));
		}
	}

}